//
//  AppDelegate.m
//  XGNotifyJump
//
//  Created by 小果 on 16/8/22.
//  Copyright © 2016年 小果. All rights reserved.
//

#import "AppDelegate.h"
#import "XGTabBarController.h"
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [self setupNotificationCategory];
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    UIImage *oldImage = [UIImage imageNamed:@"bgImage.jpg"];
    UIGraphicsBeginImageContextWithOptions(self.window.size, NO, 0.0);
    [oldImage drawInRect:CGRectMake(0, 0, self.window.width, self.window.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    self.window.backgroundColor = [UIColor colorWithPatternImage:newImage];
    
    XGTabBarController *tabBar = [[XGTabBarController alloc] init];
    self.window.rootViewController = tabBar;
    [self.window makeKeyAndVisible];
    
    [self judeWithLocalNotification:launchOptions];
    return YES;
}

#pragma mark - 设置通知的分类
-(void)setupNotificationCategory{
    //1.1 创建分类按钮 --> 一定要使用可变的子类来创建
    UIMutableUserNotificationAction *action1 = [UIMutableUserNotificationAction new];
    
    //1.1.1 设置标示符
    action1.identifier = @"Foreground";
    
    //1.1.2 设置标题
    action1.title = @"前台处理";
    
    //1.1.3 设置模式 --> 前台/后台  前台会唤醒程序, 后台会自动默默的处理不会打开程序
    action1.activationMode = UIUserNotificationActivationModeForeground;
    
    //1.2 创建分类按钮 --> 一定要使用可变的子类来创建
    UIMutableUserNotificationAction *action2 = [UIMutableUserNotificationAction new];
    
    //1.2.1 设置标示符
    action2.identifier = @"Background";
    
    //1.2.2 设置标题
    action2.title = @"后台处理";
    
    //1.2.3 设置模式
    action2.activationMode = UIUserNotificationActivationModeBackground;
    
    
    //2. 创建用户通知分类 --> 一定要使用可变的子类来创建
    UIMutableUserNotificationCategory *category = [UIMutableUserNotificationCategory new];
    
    //2.1 设置标示符
    category.identifier = @"category";
    
    //2.2 添加按钮 最多就2个
    [category setActions:@[action1,action2] forContext:UIUserNotificationActionContextDefault];
    
    
    //3. 创建set
    NSSet *set = [NSSet setWithObjects:category, nil];
    
    
    // 注册推送通知
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:set];
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}

#pragma mark - 判断通知中的值
-(void)judeWithLocalNotification:(NSDictionary *)dict{
    //1. 判断, 如果本地通知的值为空, 就不调用下面的方法
    if (dict[UIApplicationLaunchOptionsLocalNotificationKey]) {
        /*
         //创建一个Label, 让Label显示文字
         //        UILabel *label = [UILabel new];
         //        label.text = launchOptions.description;
         //        label.numberOfLines = 0;
         //        label.frame = CGRectMake(0, 20, [UIScreen mainScreen].bounds.size.width, 300);
         //        label.backgroundColor = [UIColor redColor];
         //        [self.window.rootViewController.view addSubview:label];
         */
        //2. 当程序启动的时候, 获取本地通知的值
        UILocalNotification *notification = dict[UIApplicationLaunchOptionsLocalNotificationKey];
        
        //3. 调用公用的切换控制器的方法
        [self changeChildVC:notification];
    }
    
}

#pragma mark - 接收本地通知的方法 程序如果是推出, 则不会执行此方法
//程序的状态 : 后台(程序正在运行, 但是不在主界面显示)
//           前台(程序正在运行, 并且在主界面显示)
//           挂了 (未启动)

//didReceiveLocalNotification: 当收到本地通知之后会调用的方法
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    
    /**
     UIApplicationStateActive,      激活/前台
     UIApplicationStateInactive,
     UIApplicationStateBackground   后台
     */
    //1. 获取程序的状态  --> 如果程序在前台, 我们不主动处理界面跳转
    if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive ) {
        //在前台, 直接返回
        //可以给用户弹出提示框
        return;
    }
    
    //2. 调用公用的切换控制器的方法
    [self changeChildVC:notification];
}

#pragma mark - 公用的切换控制器的方法
- (void)changeChildVC:(UILocalNotification *)notification
{
    //2. 获取通知的值
    NSInteger selectIndex = [notification.userInfo[@"selectIndex"] integerValue];
    
    //3. 获取根控制器
    UITabBarController *tabBarC = (UITabBarController *)self.window.rootViewController;
    
    //4. 根据索引切换控制器
    tabBarC.selectedViewController = tabBarC.childViewControllers[selectIndex];
}
#pragma mark - 处理分类按钮的方法
//identifier: 设置action的标示符
//completionHandler: 回调 --> 这个block必须调用一次
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler
{
    //判断标示符
    NSLog(@"id: %@", identifier);
    
    //必须调用blcok --> 苹果系统内部会自己调度资源. 苹果只是希望知道我们在什么时候把这个方法处理完毕, 处理完毕告诉苹果一声即可
    
    completionHandler(
    
    );
    
}


@end
